package com.prince.web.controller;


import com.prince.aspect.AppLog;
import com.prince.model.Config;
import com.prince.model.User;
import com.prince.page.Page;
import com.prince.web.base.BaseController;
import com.prince.web.service.ConfigService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;


@Controller
public class ConfigController extends BaseController{

	@Resource
	private ConfigService configService;
	
	/**
	 * 分页查询
	 * @param params
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "findConfigByPage", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> findConfigByPage(@RequestParam Map<String,Object> params) {
		Page page = getRequestPageRollInfo(params);
		Map<String,Object> map = configService.findConfigByPage(params,page);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 添加
	 * @param config
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "addConfig", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> addConfig(HttpServletRequest request,Config config) {
		
		
		//获取session中用户
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		//封装创建人和机构号
		config.setCreater(user.getLoginName());
		Map<String,Object> map = configService.addConfig(config);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 修改
	 * @param config
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "updateConfig", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> updateConfig(Config config) {
		Map<String,Object> map = configService.updateConfig(config);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 删除
	 * @param config
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "delConfig", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> delConfig(Config config) {
		Map<String,Object> map = configService.delConfig(config);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	
	/**
	 * 根据条件查询Config
	 * @param config
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "findConfigByCondition", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> findConfigByCondition(Config config) {
		Map<String,Object> map = configService.findConfigByCondition(config);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	/**
	 * 根据条件模糊查询Config
	 * @param config
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "dqueryConfig", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> dqueryConfig(Config config) {
		Map<String,Object> map = configService.dqueryConfig(config);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 查询字典表所有sortcode 和code
	 * @return
	 */
	@AppLog
	@RequestMapping(value="findConfigListAndMap",method=RequestMethod.GET)
	public ResponseEntity<Map<String,Object>> findCodeListAndMap(){
		Map<String,Object> map = configService.findConfigListAndMap();
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}

	@AppLog
	@RequestMapping(value = "findConfigByType", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> findConfigByType(){
		String type="active";
		Map<String, Object> map = configService.findConfigByType(type);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
}
